package nl.weeaboo.sh.editor.area;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JPanel;

import nl.weeaboo.sh.model.AreaModel;
import nl.weeaboo.sh.model.RoomModel;
import nl.weeaboo.sh.model.TileModel;

@SuppressWarnings("serial")
public class AttributePanel extends JPanel {

	private List<ChangeListener> listeners;
	
	private TileEditor tileEditor;
	private RoomEditor roomEditor;
	private Object selected;
	
	public AttributePanel() {
		listeners = new LinkedList<ChangeListener>();
		tileEditor = new TileEditor();
		roomEditor = new RoomEditor();
		roomEditor.addChangeListener(new ChangeListener() {
			public void onChanged(Object object) {
				onChange(object);
			}
		});
		
		setPreferredSize(new Dimension(200, 200));
		setLayout(new BorderLayout(5, 5));
	}
	
	//Functions
	public void addChangeListener(ChangeListener l) {
		listeners.add(l);
	}
	public void removeChangeListener(ChangeListener l) {
		listeners.remove(l);
	}
	protected void onChange(final Object obj) {
		ChangeListener ls[] = listeners.toArray(new ChangeListener[listeners.size()]);
		for (ChangeListener l : ls) {
			l.onChanged(obj);
		}
	}
	
	//Getters
	
	//Setters
	public void setSelected(AreaModel area, Object object) {
		if (selected != object) {
			selected = object;
			
			removeAll();
			
			if (selected instanceof TileModel) {
				add(tileEditor, BorderLayout.NORTH);
				tileEditor.setSelectedTile((TileModel)selected);				
				tileEditor.invalidate();
			} else if (selected instanceof RoomModel) {
				add(roomEditor, BorderLayout.NORTH);
				roomEditor.setSelectedRoom(area, (RoomModel)selected);
				roomEditor.invalidate();
			}

			validate();
			repaint();
		}
	}
	
}
